﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using log4net;
using System.Reflection;
using System.IO;

namespace SopCastRecorder
{
	public partial class EditorForm : Form
	{
		private static ILog logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

		public string Path { get; set; }

		public EditorForm()
		{
			InitializeComponent();
		}

		private void EditorForm_Load(object sender, EventArgs e)
		{
			try
			{
				this.Init();
			}
			catch (Exception exception1)
			{
				logger.Error(MethodBase.GetCurrentMethod().Name, exception1);
				Form1.SetErrorStatus();
			}
		}

		private void Init()
		{
			if (!File.Exists(this.Path))
			{
				return;
			}

			try
			{
				using (var reader = new StreamReader(this.Path, Encoding.Unicode))
				{
					this.textBox1.Text = reader.ReadToEnd();
					reader.Close();
				}
			}
			catch
			{
			}
		}

		private void btnSave_Click(object sender, EventArgs e)
		{
			try
			{
				this.Save();
			}
			catch (Exception exception1)
			{
				logger.Error(MethodBase.GetCurrentMethod().Name, exception1);
				Form1.SetErrorStatus();
			}
		}

		private void Save()
		{
			using (var writer = new StreamWriter(this.Path, false, Encoding.Unicode))
			{
				writer.Write(this.textBox1.Text);
				writer.Close();
			}

			this.Close();
		}
	}
}
